package com.seselin.citypicker.db;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.seselin.citypicker.bean.Area;
import com.seselin.citypicker.bean.City;
import com.seselin.citypicker.bean.Province;

import java.util.ArrayList;

/**
 * Created by Seselin on 2016/4/12.
 * <p>
 * 数据库操作
 */
public class CityDataBase {

    private static volatile CityDataBase instance;
    private static String TABLE_NAME = "area";

    private SQLiteDatabase database;

    public static CityDataBase getInstance(Context context) {
        if (instance == null) {
            synchronized (CityDataBase.class) {
                if (instance == null) {
                    instance = new CityDataBase(context);
                }
            }
        }
        return instance;
    }

    public CityDataBase(Context context) {
        AssetsDatabaseManager.initManager(context);
        AssetsDatabaseManager manager = AssetsDatabaseManager.getManager();
        database = manager.getDatabase("city_db3.db");
    }

    public ArrayList<Province> getProvinceList() {
        ArrayList<Province> provinces = new ArrayList<>();
        Cursor cursor = database.query(TABLE_NAME, null, "pid=?", new
                String[]{"0"}, null, null, null);
        while (cursor.moveToNext()) {
            String id = cursor.getString(cursor.getColumnIndex("id"));
            String name = cursor.getString(cursor.getColumnIndex("name"));
            String pid = cursor.getString(cursor.getColumnIndex("pid"));
            String nid = cursor.getString(cursor.getColumnIndex("nid"));
            Province province = new Province(id, name, pid, nid);
            provinces.add(province);
        }
        cursor.close();
        return provinces;
    }

    public ArrayList<City> getCityList(String myId) {
        ArrayList<City> cities = new ArrayList<>();
        Cursor cursor = database.query(TABLE_NAME, null, "pid=?", new
                String[]{myId}, null, null, null);
        while (cursor.moveToNext()) {
            String id = cursor.getString(cursor.getColumnIndex("id"));
            String name = cursor.getString(cursor.getColumnIndex("name"));
            String pid = cursor.getString(cursor.getColumnIndex("pid"));
            String nid = cursor.getString(cursor.getColumnIndex("nid"));
            City province = new City(id, name, pid, nid);
            cities.add(province);
        }
        cursor.close();
        return cities;
    }

    public ArrayList<Area> getAreaList(String myId) {
        ArrayList<Area> arrayList = new ArrayList<>();
        Cursor cursor = database.query(TABLE_NAME, null, "pid=?", new
                String[]{myId}, null, null, null);
        while (cursor.moveToNext()) {
            String id = cursor.getString(cursor.getColumnIndex("id"));
            String name = cursor.getString(cursor.getColumnIndex("name"));
            String pid = cursor.getString(cursor.getColumnIndex("pid"));
            String nid = cursor.getString(cursor.getColumnIndex("nid"));
            Area province = new Area(id, name, pid, nid);
            if (!"市辖区".equals(name))
                arrayList.add(province);
        }
        cursor.close();
        return arrayList;
    }


}
